/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.ybk.e2p.yb.service;

import cn.ac.iie.di.ban.ycl.ybk.e2p.yb.tools.fastutil.FastPo;
import cn.ac.iie.di.ban.ycl.ybk.e2p.yb.tools.fastutil.FileManagerUtil;
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import org.apache.log4j.Logger;

/**
 *
 * @author root
 */
public class FastThreadPoolTask implements Runnable, Serializable {

	private static Logger logger = Logger.getLogger(FastThreadPoolTask.class);
	private String localFilePath;
	private FastUploadService fastUploadService;
	private HashMap<String, FastPo> retStringMap;

	FastThreadPoolTask(String localFilePath, FastUploadService fastUploadService) {
		this.localFilePath = localFilePath;
		this.fastUploadService = fastUploadService;
	}

	FastThreadPoolTask(String localFilePath, HashMap<String, FastPo> retStringMap, FastUploadService fastUploadService) {
		this.localFilePath = localFilePath;
		this.retStringMap = retStringMap;
		this.fastUploadService = fastUploadService;
	}

	public void run() {
		logger.info(" start to execute FastThreadPoolTask!");
		FileManagerUtil fileManagerUtil = new FileManagerUtil();
		FastPo fastPo = fileManagerUtil.upload(localFilePath);
		boolean flag = fastPo.isUpFlag();
		String path = null;
		if (!flag) {
			path = fastPo.getFilePath();
                        String retStringMapKey = new File(localFilePath).getName().split("\\.")[0];
			retStringMap.put(retStringMapKey, fastPo);
//			retStringMap.put(new File(localFilePath).getName(), path);
			fastUploadService.changeDesc( localFilePath + " fast upload error!");
			fastUploadService.changeStatus("fail");
			logger.info(  localFilePath + " fast upload error!");
		} else {
			path = fastPo.getFilePath();
                        String retStringMapKey = new File(localFilePath).getName().split("\\.")[0];
			retStringMap.put(retStringMapKey, fastPo);
//			retStringMap.put(new File(localFilePath).getName(), path);
			logger.info(" the return path of FastDFS: " + path);
			fastUploadService.changeStatus("sucess");
			fastUploadService.plusSucessUploadFileNumber();
			logger.info( localFilePath + " fast upload sucess");
		}
	}

	/**
	 * @return the fastUploadService
	 */
	public FastUploadService getFastUploadService() {
		return fastUploadService;
	}

	/**
	 * @param fastUploadService the fastUploadService to set
	 */
	public void setFastUploadService(FastUploadService fastUploadService) {
		this.fastUploadService = fastUploadService;
	}

	/**
	 * @return the localFilePath
	 */
	public String getLocalFilePath() {
		return localFilePath;
	}

	/**
	 * @param localFilePath the localFilePath to set
	 */
	public void setLocalFilePath(String localFilePath) {
		this.localFilePath = localFilePath;
	}

	/**
	 * @return the pathList
	 */
	public HashMap<String, FastPo> getRetStringMap() {
		return retStringMap;
	}

	/**
	 * @param retStringMap the pathList to set
	 */
	public void setRetStringMap(HashMap<String, FastPo> retStringMap) {
		this.retStringMap = retStringMap;
	}
}
